import { build, defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteObfuscateFile } from 'vite-plugin-obfuscator'

const proxyHosts = {
  dev: 'http://34.143.240.186:8300/',
  dev2: 'http://34.87.43.208:8000/',
  // prod: 'https://blockjoker.org/',
  prod: 'https://wait.blockjoker.org/'
}

// const noAttr = () => {
//   return {
//     name: "no-attribute",
//     transformIndexHtml(html) {
//       return html.replace(`type="module" crossorigin`, "");
//     }
//   }
// }

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const envDir = '/env'
  const env = loadEnv(mode, process.cwd() + envDir, 'VITE_')
  console.log('构建环境变量：', env)
  return {
    define: {
      __BUILD_TIME__: `${Date.now()}`
    },
    envDir: `.${envDir}`,
    base: env['VITE_BASE'],
    plugins: [
      vue(),
      viteObfuscateFile({
        compact: true,
        controlFlowFlattening: false,
        controlFlowFlatteningThreshold: 0.75,
        deadCodeInjection: false,
        deadCodeInjectionThreshold: 0.4,
        debugProtection: false,
        debugProtectionInterval: 0,
        disableConsoleOutput: false,
        domainLock: [],
        domainLockRedirectUrl: 'about:blank',
        forceTransformStrings: [],
        identifierNamesCache: null,
        identifierNamesGenerator: 'hexadecimal',
        identifiersDictionary: [],
        identifiersPrefix: '',
        ignoreImports: false,
        inputFileName: '',
        log: false,
        numbersToExpressions: false,
        optionsPreset: 'default',
        renameGlobals: false,
        renameProperties: false,
        renamePropertiesMode: 'safe',
        reservedNames: [],
        reservedStrings: [],
        seed: 0,
        selfDefending: false,
        simplify: true,
        sourceMap: false,
        sourceMapBaseUrl: '',
        sourceMapFileName: '',
        sourceMapMode: 'separate',
        sourceMapSourcesMode: 'sources-content',
        splitStrings: false,
        splitStringsChunkLength: 10,
        stringArray: true,
        stringArrayCallsTransform: true,
        stringArrayCallsTransformThreshold: 0.5,
        stringArrayEncoding: [],
        stringArrayIndexesType: ['hexadecimal-number'],
        stringArrayIndexShift: true,
        stringArrayRotate: true,
        stringArrayShuffle: true,
        stringArrayWrappersCount: 1,
        stringArrayWrappersChainedCalls: true,
        stringArrayWrappersParametersMaxCount: 2,
        stringArrayWrappersType: 'variable',
        stringArrayThreshold: 0.75,
        target: 'browser',
        transformObjectKeys: false,
        unicodeEscapeSequence: false,
      }),
      // noAttr()
    ],
    build: {
      outDir: 'dist'
    },
    server: {
      // host: '192.168.3.224',
      port: 8001,
      proxy: {
        '/api': {
          target: `${proxyHosts.dev2}`,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
      }
    },
    css: {
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import "./src/assets/styles/variables.less";'
        },
        scss: {
          charset: false
        }
      }
    }
  }
})
